// 
//  queryobserver.js
//  firefox
//  
//  Created by Zak on 2008-06-09.
//  Copyright 2008 Ant.com. All rights reserved.
// 

/**
 * Class: Observer called when firefox download something
 * @constructor     No parameters
 */
top.AntQueryObserver = function ()
{
    this.validContentTypes = {
        "video/flv":1,
        "video/x-flv":1,
        "video/f4v":1,
        "video/x-flash-video":1,
        "video/mp4":1
    }

    this.possibleContentTypes = {
        "application/x-shockwave-flash":1,
        "application/octet-stream":1,
        "text/plain":1
    }
    
    /**
     * Is this URL a junk URL ? From a ads for exemple
     * @param url           The URL to check
     * @return boolean
     */
    this.isJunkFlv = function (request)
    {
        return request.name.match(/ads|advert/);
    }
    
    /**
     * Is this URL a valid URL ? Assuming the content type as been checked first
     * @param url           The URL to check
     * @return boolean
     */
    this.isValidFlv = function (url)
    {
        return url.match(/flv|video|movie|film|clip|f4v|vod|mp4/) && url.match(/.flv|.f4v|.mp4/);
    }
    
    /**
     * Is this URL the result of a seek in a player ?
     * @param url           The URL to check
     * @return boolean
     */
    this.isSeekUrl = function (url)
    {
        // &fs=XXX => seek dailymotion
        return url.match(/[?&](st(art)?|begin|fs)=([1-9][0-9]*|0+[1-9]+)/i);
    }

    /**
     * Check if the given request is a valid flv file
     * @param request       The request object to authentify
     * @return boolean      True if the request is for a valid flv file, false instead
     */
    this.isFlv = function (request)
    {
        if (this.isSeekUrl(request.name))
        {
            AntLib.toLog(request.name + " Rejected => Seek URL");
            return false;
        }
        if (this.validContentTypes[request.contentType] && !this.isJunkFlv(request))
        {
            AntLib.toLog(request.name + " Accepted => True Content & no Junk");
            return true;
        }
        if (this.possibleContentTypes[request.contentType]
                && !this.isJunkFlv(request)
                && this.isValidFlv(request.name))
        {
            AntLib.toLog(request.name + " Accepted => Possible Content & no Junk & Valid");
            return true;
        }
        return false;
    }
    
    this.getDocumentByRequest = function (request)
    {
        if (request.notificationCallbacks)
        {
            if (!(request.notificationCallbacks instanceof Components.interfaces.nsIDocShell))
                return null;
            try {
                var interfaceRequestor = QI(request.notificationCallbacks, CI("nsIInterfaceRequestor"));
                var win = GI(interfaceRequestor, CI("nsIDOMWindow"));
                var currentDoc = win.document;
                var rootDoc = currentDoc;

                while (rootDoc.defaultView.frameElement)
                    rootDoc = rootDoc.defaultView.frameElement.ownerDocument;

                return rootDoc;
            }
            catch (err) {
                return null;
            }
        }
        return null;
    }

    /**
     * The function called by firefox, entry point of this class
     * @param request       The object containing the request params
     * @param topic         The specific event that trigger the function
     * @param data          the request data (seems to be empty)
     */
    this.observe = function (request, topic, data)
    {
        var request = request.QueryInterface(Components.interfaces["nsIHttpChannel"]);
        var doc = this.getDocumentByRequest(request);
        
        if (doc == null)
            return ;
		if (AntPrefs.isExpertMode() && !AntPrefs.isSiteEnabled(doc.location))
			return;
        if (this.isFlv(request))
        {
    		var origin = AntLib.getSiteName(doc.location);
            var url = request.name;
    		var name = safeGet(doc, "title");
            var flvLink = new AntFlvLink({origin:origin, url:url, name:name, doc:doc});

            AntGrabber.foundFlvLink(flvLink);
        }
    };  
}

